package com.kamenrider.adventureaffiliated.procedures;

import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.common.MinecraftForge;

import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.MobSpawnType;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.util.Mth;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.core.particles.ParticleTypes;

import java.util.Random;

import com.kamenrider.adventureaffiliated.init.RiderAdventureAffiliatedModItems;
import com.kamenrider.adventureaffiliated.init.RiderAdventureAffiliatedModEntities;
import com.kamenrider.adventureaffiliated.entity.AnotherBuild2019Entity;
import com.kamenrider.adventureaffiliated.entity.AnotherBuild2017Entity;

public class AnotherbuildwatchDangHuoZhaoDeShiTiBeiGaiWuPinJiZhongProcedure {
	public static void execute(LevelAccessor world, double x, double y, double z, Entity entity, Entity sourceentity) {
		if (entity == null || sourceentity == null)
			return;
		if ((ForgeRegistries.ENTITIES.getKey(entity.getType()).toString()).equals("minecraft:villager")) {
			if (entity instanceof LivingEntity _entity)
				_entity.addEffect(new MobEffectInstance(MobEffects.MOVEMENT_SLOWDOWN, 60, 17, (false), (false)));
			if (entity instanceof LivingEntity _entity)
				_entity.addEffect(new MobEffectInstance(MobEffects.DAMAGE_RESISTANCE, 60, 17, (false), (false)));
			if (world instanceof ServerLevel _level)
				_level.sendParticles(ParticleTypes.SOUL_FIRE_FLAME, x, (y + Mth.nextDouble(new Random(), 0, 1)), z, 20, 0.5, 0.5, 0.5, 0.5);
			if (world instanceof ServerLevel _level)
				_level.sendParticles(ParticleTypes.CAMPFIRE_COSY_SMOKE, x, (y + Mth.nextDouble(new Random(), 0, 1)), z, 20, 0.5, 0.5, 0.5, 0.5);
			if (world instanceof ServerLevel _level)
				_level.sendParticles(ParticleTypes.SOUL, x, (y + Mth.nextDouble(new Random(), 0, 1)), z, 20, 0.5, 0.5, 0.5, 0.5);
			if (world instanceof ServerLevel _level)
				_level.sendParticles(ParticleTypes.EXPLOSION_EMITTER, x, (y + Mth.nextDouble(new Random(), 0, 1)), z, 1, 0.5, 0.5, 0.5, 0.5);
			if (world instanceof ServerLevel _level) {
				Entity entityToSpawn = new AnotherBuild2017Entity(RiderAdventureAffiliatedModEntities.ANOTHER_BUILD_2017.get(), _level);
				entityToSpawn.moveTo(x, y, z, world.getRandom().nextFloat() * 360F, 0);
				if (entityToSpawn instanceof Mob _mobToSpawn)
					_mobToSpawn.finalizeSpawn(_level, world.getCurrentDifficultyAt(entityToSpawn.blockPosition()), MobSpawnType.MOB_SUMMONED, null, null);
				world.addFreshEntity(entityToSpawn);
			}
			new Object() {
				private int ticks = 0;
				private float waitTicks;
				private LevelAccessor world;

				public void start(LevelAccessor world, int waitTicks) {
					this.waitTicks = waitTicks;
					MinecraftForge.EVENT_BUS.register(this);
					this.world = world;
				}

				@SubscribeEvent
				public void tick(TickEvent.ServerTickEvent event) {
					if (event.phase == TickEvent.Phase.END) {
						this.ticks += 1;
						if (this.ticks >= this.waitTicks)
							run();
					}
				}

				private void run() {
					if (world instanceof ServerLevel _level)
						_level.sendParticles(ParticleTypes.SOUL_FIRE_FLAME, x, (y + Mth.nextDouble(new Random(), 0, 1)), z, 10, 0.5, 0.5, 0.5, 0.5);
					if (world instanceof ServerLevel _level)
						_level.sendParticles(ParticleTypes.LARGE_SMOKE, x, (y + Mth.nextDouble(new Random(), 0, 1)), z, 10, 0.5, 0.5, 0.5, 0.5);
					if (world instanceof ServerLevel _level)
						_level.sendParticles(ParticleTypes.CLOUD, x, (y + Mth.nextDouble(new Random(), 0, 1)), z, 5, 0.5, 0.5, 0.5, 0.5);
					MinecraftForge.EVENT_BUS.unregister(this);
				}
			}.start(world, 5);
			if (!entity.level.isClientSide())
				entity.discard();
			if (sourceentity instanceof Player _player) {
				ItemStack _stktoremove = new ItemStack(RiderAdventureAffiliatedModItems.ANOTHER_BUILD_WATCH.get());
				_player.getInventory().clearOrCountMatchingItems(p -> _stktoremove.getItem() == p.getItem(), 1, _player.inventoryMenu.getCraftSlots());
			}
		} else if ((ForgeRegistries.ENTITIES.getKey(entity.getType()).toString()).equals("minecraft:pillager") || (ForgeRegistries.ENTITIES.getKey(entity.getType()).toString()).equals("minecraft:evoker")
				|| (ForgeRegistries.ENTITIES.getKey(entity.getType()).toString()).equals("minecraft:vindicator")) {
			if (entity instanceof LivingEntity _entity)
				_entity.addEffect(new MobEffectInstance(MobEffects.MOVEMENT_SLOWDOWN, 60, 17, (false), (false)));
			if (entity instanceof LivingEntity _entity)
				_entity.addEffect(new MobEffectInstance(MobEffects.DAMAGE_RESISTANCE, 60, 17, (false), (false)));
			if (world instanceof ServerLevel _level)
				_level.sendParticles(ParticleTypes.SOUL_FIRE_FLAME, x, (y + Mth.nextDouble(new Random(), 0, 1)), z, 10, 0.5, 0.5, 0.5, 0.5);
			if (world instanceof ServerLevel _level)
				_level.sendParticles(ParticleTypes.LARGE_SMOKE, x, (y + Mth.nextDouble(new Random(), 0, 1)), z, 10, 0.5, 0.5, 0.5, 0.5);
			if (world instanceof ServerLevel _level)
				_level.sendParticles(ParticleTypes.CLOUD, x, (y + Mth.nextDouble(new Random(), 0, 1)), z, 5, 0.5, 0.5, 0.5, 0.5);
			if (world instanceof ServerLevel _level) {
				Entity entityToSpawn = new AnotherBuild2019Entity(RiderAdventureAffiliatedModEntities.ANOTHER_BUILD_2019.get(), _level);
				entityToSpawn.moveTo(x, y, z, world.getRandom().nextFloat() * 360F, 0);
				if (entityToSpawn instanceof Mob _mobToSpawn)
					_mobToSpawn.finalizeSpawn(_level, world.getCurrentDifficultyAt(entityToSpawn.blockPosition()), MobSpawnType.MOB_SUMMONED, null, null);
				world.addFreshEntity(entityToSpawn);
			}
			new Object() {
				private int ticks = 0;
				private float waitTicks;
				private LevelAccessor world;

				public void start(LevelAccessor world, int waitTicks) {
					this.waitTicks = waitTicks;
					MinecraftForge.EVENT_BUS.register(this);
					this.world = world;
				}

				@SubscribeEvent
				public void tick(TickEvent.ServerTickEvent event) {
					if (event.phase == TickEvent.Phase.END) {
						this.ticks += 1;
						if (this.ticks >= this.waitTicks)
							run();
					}
				}

				private void run() {
					if (world instanceof ServerLevel _level)
						_level.sendParticles(ParticleTypes.SOUL_FIRE_FLAME, x, (y + Mth.nextDouble(new Random(), 0, 1)), z, 10, 0.5, 0.5, 0.5, 0.5);
					if (world instanceof ServerLevel _level)
						_level.sendParticles(ParticleTypes.LARGE_SMOKE, x, (y + Mth.nextDouble(new Random(), 0, 1)), z, 10, 0.5, 0.5, 0.5, 0.5);
					if (world instanceof ServerLevel _level)
						_level.sendParticles(ParticleTypes.CLOUD, x, (y + Mth.nextDouble(new Random(), 0, 1)), z, 5, 0.5, 0.5, 0.5, 0.5);
					MinecraftForge.EVENT_BUS.unregister(this);
				}
			}.start(world, 5);
			if (!entity.level.isClientSide())
				entity.discard();
			if (sourceentity instanceof Player _player) {
				ItemStack _stktoremove = new ItemStack(RiderAdventureAffiliatedModItems.ANOTHER_BUILD_WATCH.get());
				_player.getInventory().clearOrCountMatchingItems(p -> _stktoremove.getItem() == p.getItem(), 1, _player.inventoryMenu.getCraftSlots());
			}
		}
	}
}
